<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/7/14
 * Time: 11:00 AM
 */

namespace Admin\Model;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Admin\Model\AdminFriends;

class AdminFriendsTable
{
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }


    public function fetchAll($user_id = null)
    {
        $select = new Select();
        $select->columns(array(
            'id' => 'id',
            'user_id' => 'user_id',
            'friend_id' => 'friend_id',
            'follow' => 'follow',
        ));
        $select->from(array('f' => 'friend'));
        $select->join(array('u' => 'user'),'f.friend_id = u.id',array('name'=>'name','email'=>'email','avatar'=>'avatar'));
        $select->where(array('f.user_id ='.$user_id));
        $select->order('follow DESC');

        $resultSetPrototype = new ResultSet();
        //die;
        $resultSetPrototype->setArrayObjectPrototype(new AdminFriends());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }
    
    public function deleteFriend($id){
        $this->tableGateway->delete(array('id' => $id));
    }

    public function addFriend($friend = array()){

        $adapter = $this->tableGateway->getAdapter();
        $sql = new Sql($adapter);
        $queryString='';
        $id = null;
        if(!isset($friend['id'])){
            $insert = $sql->insert('friend');
            $insert->values(array('user_id'=>$friend['user_id'], 'friend_id'=>$friend['friend_id'], 'follow'=>$friend['follow']));
            $queryString = $sql->getSqlStringForSqlObject($insert);
        }
        else{
            $id = $friend['id'];
            $update = $sql->update(array('user_id'=>$friend['user_id'], 'friend_id'=>$friend['friend_id'], 'follow'=>$friend['follow']))
                ->where(array('id'=>$friend['id']));
            $queryString = $sql->getSqlStringForSqlObject($update);
        }
        $stm = $adapter->query($queryString);
        $res = $stm->execute();
        if($id==null)
            $id = $res->getGeneratedValue();
        return $id;
    }

    public function checkFriend($user_id, $friend_id){
        $select = new Select();
        $select->from(array('f' => 'friend'));
        $select->where(array('f.user_id ='.$user_id, 'f.friend_id = '.$friend_id));

        $adapter = $this->tableGateway->getAdapter();
        $sql = new Sql($adapter);

        $sqlString = $sql->getSqlStringForSqlObject($select);
        $stm = $adapter->query($sqlString);
        $result = $stm->execute();
        if($result->count()){
            return true;
        }
        return false;
    }

    public function getFriendTopic($user_id, $from_row, $to_row){

        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute('CALL fetch_new_friend_topic('.$user_id.','.$from_row.','.$to_row.')');
        $statement = $result->getResource();
       $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
       // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;

    }
    public function getFriendByUser($userid,$from,$to){
        $str = 'SELECT * FROM friend WHERE `user_id` = '.$userid.' ORDER BY id DESC limit '.$from.' ,'.$to;
        $driver = $this->tableGateway->getAdapter()->getDriver();
        $connection = $driver->getConnection();
        $result = $connection->execute($str);
        $statement = $result->getResource();
        $resultSet1 = $statement->fetchAll(\Pdo::FETCH_NAMED);
        // $resultSet1 = $statement->fetchAll(\Pdo::FETCH_OBJ);
        return $resultSet1;
    }
}